<template>
  <div class="login-container">
    <div class="login-form">
      <div class="form-wrapper">
        <div class="login-title">用户<span class="app-name">注册</span></div>
        <el-form :model="userForm" :rules="rules" ref="userForm" label-width="100px" class="register-form-content">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="userForm.username" prefix-icon="el-icon-user"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="userForm.password" prefix-icon="el-icon-lock" type="password" show-password></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="confirmPassword">
            <el-input v-model="userForm.confirmPassword" prefix-icon="el-icon-lock" type="password"
                      show-password></el-input>
          </el-form-item>
          <el-form-item label="昵称" prop="nickname">
            <el-input v-model="userForm.nickname" prefix-icon="el-icon-user"></el-input>
          </el-form-item>
          <el-form-item label="地址" prop="address">
            <el-input v-model="userForm.address" prefix-icon="el-icon-location"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="userForm.email" prefix-icon="el-icon-message"></el-input>
          </el-form-item>
          <el-form-item label="联系方式" prop="phone">
            <el-input v-model="userForm.phone" prefix-icon="el-icon-phone"></el-input>
          </el-form-item>
          <el-form-item class="btn-container">
            <el-button type="primary" @click="register('userForm')" class="register-btn">注册</el-button>
            <el-button type="text" @click="$router.push('/login')" class="login-link-btn">已有账号？返回登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>

  <!--  <div class="wrapper">-->

  <!--&lt;!&ndash;    项目名称&ndash;&gt;-->
  <!--    <div style="display: flex;width:55%;height:50%;margin: 150px auto;background-color: white;border-radius: 10px;overflow: hidden;background-color: bisque">-->
  <!--&lt;!&ndash;      左侧图片显示&ndash;&gt;-->
  <!--      <div style="width: 50%;margin-top: 30px;margin-left: 30px">-->
  <!--        <img src="../assets/register.png" alt="" style="width:100%;height:90%" />-->
  <!--      </div>-->
  <!--&lt;!&ndash;      提交表单&ndash;&gt;-->
  <!--      <div style="width:350px;margin-top: 30px">-->
  <!--        <div style="margin-top: 20px;text-align: center;font-size: 20px;margin-bottom: 10%">注册页面</div>-->
  <!--        <el-form :model="userForm" :rules="rules" ref="userForm" label-width="100px" class="demo-ruleForm">-->
  <!--          <el-form-item label="用户名" prop="username">-->
  <!--            <el-input v-model="userForm.username" prefix-icon="el-icon-user"></el-input>-->
  <!--          </el-form-item>-->
  <!--          <el-form-item label="密码" prop="password">-->
  <!--            <el-input v-model="userForm.password" prefix-icon="el-icon-lock" type="password" show-password></el-input>-->
  <!--          </el-form-item>-->
  <!--          <el-form-item label="确认密码" prop="confirmPassword">-->
  <!--            <el-input v-model="userForm.confirmPassword" prefix-icon="el-icon-lock" type="password" show-password></el-input>-->
  <!--          </el-form-item>-->
  <!--          <el-form-item>-->
  <!--            <el-button type="primary" @click="register('userForm')">注册</el-button>-->
  <!--            <el-button type="success" @click="$router.push('/login')">返回登录</el-button>-->
  <!--&lt;!&ndash;            <el-button type="warning" @click="resetForm('userForm')">重置</el-button>&ndash;&gt;-->
  <!--          </el-form-item>-->
  <!--        </el-form>-->
  <!--      </div>-->
  <!--    </div>-->
  <!--  </div>-->
</template>

<script>
export default {
  name: "Register",
  data() {
    return {
      userForm: {
        username: '',
        password: '',
        confirmPassword: '',
        nickname: '',
        address: '',
        email: '',
        phone: '',
        role_id: 2  // 默认注册为普通用户
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名称', trigger: 'blur'},
          {min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, max: 50, message: '长度不能小于6位', trigger: 'blur'}
        ],
        confirmPassword: [
          {required: true, message: '请输入确认密码', trigger: 'blur'},
          {min: 6, max: 50, message: '长度不能小于6位', trigger: 'blur'}
        ],
        nickname: [
          {required: true, message: '请输入昵称', trigger: 'blur'},
          {min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
        ],
        address: [
          {required: true, message: '请输入地址', trigger: 'blur'},
          {max: 255, message: '长度不能超过255个字符', trigger: 'blur'}
        ],
        email: [
          {required: true, message: '请输入邮箱', trigger: 'blur'},
          {type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '请输入联系方式', trigger: 'blur'},
          {pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    //提交数据
    register(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.userForm.password !== this.userForm.confirmPassword) {
            this.$message.warning("两次密码不一致，请重新输入");
            return;
          }
          // 移除确认密码字段，因为后端不需要
          const { confirmPassword, ...registerData } = this.userForm;
          this.request.post("/user/register", registerData).then(res => {
            if (res.code !== '200') {
              this.$message.error(res.msg);
            } else {
              this.$message.success("注册成功，请前往登录");
              this.$router.push("/login")
            }
          })
        } else {
          console.log('输入数据不正确，请重新输入');
          return false;
        }
      });
    },
    // 重置数据
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

    // 验证码方法
    refreshCode() {
      this.identifyCode = ''
      this.makeCode(this.identifyCodes, 4);
    },
    makeCode(o, l) {
      for (let i = 0; i < l; i++) {
        this.identifyCode += this.identifyCodes[
            this.randomNum(0, this.identifyCodes.length)
            ]
      }
    },
    randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min);
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url("../assets/background.png");
  background-size: cover;
  background-position: center;
  position: relative;
}

.login-container::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(3px);
}

.login-form {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 550px;
  background-color: rgba(255, 255, 255, 0.95);
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2);
  position: relative;
  z-index: 1;
  animation: fadeIn 0.8s ease-in-out;
  padding: 40px;
  max-height: 90vh;
}

.form-wrapper {
  width: 100%;
  overflow-y: auto;
  max-height: calc(90vh - 80px);
  padding-right: 10px;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(-20px); }
  to { opacity: 1; transform: translateY(0); }
}

.login-title {
  font-size: 28px;
  margin-bottom: 30px;
  font-weight: 600;
  text-align: center;
  color: #333;
  position: relative;
}

.app-name {
  color: #409EFF;
  font-weight: 700;
  margin: 0 5px;
}

.login-title::after {
  content: "";
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 50px;
  height: 3px;
  background: #409EFF;
  border-radius: 2px;
}

.register-form-content {
  width: 100%;
}

.el-form-item {
  margin-bottom: 15px;
}

.el-input {
  margin-bottom: 5px;
}

.el-input >>> .el-input__inner {
  height: 40px;
  border-radius: 8px;
  border: 1px solid #e0e0e0;
  transition: all 0.3s;
}

.el-input >>> .el-input__inner:focus {
  border-color: #409EFF;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}

.el-form-item__label {
  font-weight: 500;
  color: #606266;
}

.btn-container {
  margin-top: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.register-btn {
  width: 100%;
  height: 45px;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 1px;
  background: linear-gradient(to right, #409EFF, #66b1ff);
  border: none;
  box-shadow: 0 5px 15px rgba(64, 158, 255, 0.3);
  transition: all 0.3s;
}

.register-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(64, 158, 255, 0.4);
  background: linear-gradient(to right, #3a8ee6, #5aa0e6);
}

.login-link-btn {
  margin-top: 15px;
  color: #606266;
  font-size: 14px;
  transition: all 0.3s;
}

.login-link-btn:hover {
  color: #409EFF;
  transform: translateY(-2px);
}

/* 设置滚动条样式 */
.form-wrapper::-webkit-scrollbar {
  width: 6px;
}

.form-wrapper::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 10px;
}

.form-wrapper::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 10px;
}

.form-wrapper::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}

@media screen and (max-width: 576px) {
  .login-form {
    width: 90%;
    padding: 30px 20px;
  }
}
</style>
